package flc.ast.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import flc.ast.R;
import me.zhouzhuo.zzratingbar.ZzRatingBar;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class RankAdapter extends StkProviderMultiAdapter<StkResBean> {

    public RankAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new RankProvider());
    }

    private class RankProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_rank;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            if (baseViewHolder.getBindingAdapterPosition() == 0) {
                baseViewHolder.setImageResource(R.id.ivRankItemIcon, R.drawable.ph1);
                baseViewHolder.getView(R.id.ivRankItemIcon).setVisibility(View.VISIBLE);
            } else if (baseViewHolder.getBindingAdapterPosition() == 1) {
                baseViewHolder.setImageResource(R.id.ivRankItemIcon, R.drawable.ph2);
                baseViewHolder.getView(R.id.ivRankItemIcon).setVisibility(View.VISIBLE);
            } else if (baseViewHolder.getBindingAdapterPosition() == 2) {
                baseViewHolder.setImageResource(R.id.ivRankItemIcon, R.drawable.ph3);
                baseViewHolder.getView(R.id.ivRankItemIcon).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.ivRankItemIcon).setVisibility(View.GONE);
            }

            Glide.with(baseViewHolder.itemView).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivRankItemImg));
            baseViewHolder.setText(R.id.tvRankItemName, bean.getName());

            String str = new Gson().toJson(bean.getExtra());

            try {
                if (str != null && !str.equals("\"\"")) {
                    JSONObject extra = new JSONObject(str);
                    JSONArray actorList = extra.optJSONArray("actorList");
                    if (actorList != null) {
                        for (int i = 0; i < actorList.length(); i++) {
                            JSONObject item = actorList.getJSONObject(i);
                            if (!item.optString("role").equals("导演")) {
                                baseViewHolder.setText(R.id.tvRankItemDirector, item.optString("name"));
                            }
                        }
                    }
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            String tag = "";
            for (int i = 0; i < bean.getTagNameList().size(); i++) {
                if (i == bean.getTagNameList().size() - 1) {
                    tag += bean.getTagNameList().get(i);
                } else {
                    tag += bean.getTagNameList().get(i) + " | ";
                }
            }
            baseViewHolder.setText(R.id.tvRankItemType, tag);

            //评分
            double score = (double) bean.getScore_total() / (bean.getScore_count() == 0 ? 1 : bean.getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvRankItemScore, formattedNumber);

            ZzRatingBar zzRatingBar = baseViewHolder.getView(R.id.zzratingbar);
            if (score > 8) {
                zzRatingBar.setRating(5);
            } else if (score > 6) {
                zzRatingBar.setRating(4);
            } else if (score > 4) {
                zzRatingBar.setRating(3);
            } else if (score > 2) {
                zzRatingBar.setRating(2);
            } else {
                zzRatingBar.setRating(1);
            }
        }
    }

}
